package com.wdf.controller;

import com.wdf.pojo.Dept;
import com.wdf.pojo.Result;
import com.wdf.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result deptList(){
        log.info("查询所有部门");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @DeleteMapping
    public Result delDept(@RequestParam("id") Integer id){
//        System.out.println("删除部门id:"+id);
        log.info("删除部门id:"+id);
        deptService.delDept(id);
        return Result.success();
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept){
//        System.out.println(dept.toString());
        log.info("新增部门dept:{}",dept);
        deptService.addDept(dept);
        System.out.println(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getDeptInfo(Integer id){
        log.info("根据部门id查询："+id);
        return Result.success(deptService.getDeptInfo(id));
    }

    @PostMapping("/update")
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门dept:{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }
}
